package com.kmust.dream.wanderingdream.donation.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.kmust.dream.wanderingdream.donation.feign.UploadAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kmust.dream.wanderingdream.donation.entity.AwardInformationDonationEntity;
import com.kmust.dream.wanderingdream.donation.service.AwardInformationDonationService;
import com.kmust.dream.wanderingdream.comm.utils.PageUtils;
import com.kmust.dream.wanderingdream.comm.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author Leng
 * @email Leng81605@gmail.com
 * @date 2021-07-12 21:35:52
 */
@RestController
@Api(tags = "受捐赠者获奖信息")
@RequestMapping("donation/award")
public class AwardInformationDonationController {
    @Autowired
    private AwardInformationDonationService awardInformationDonationService;
    @Autowired
    UploadAdvice uploadAdvice;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("带有分页信息的全部记录")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = awardInformationDonationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{awardId}")
    @ApiOperation("获得指定id的记录")
    public R info(@PathVariable("awardId") Integer awardId){
		AwardInformationDonationEntity awardInformationDonation = awardInformationDonationService.getById(awardId);

        return R.ok().put("awardInformationDonation", awardInformationDonation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")

    public R save(AwardInformationDonationEntity awardInformationDonation, @RequestParam("file")MultipartFile multipartFile){
        String url = uploadAdvice.getUrl(multipartFile);
        awardInformationDonation.setAwardImg(url);
        System.out.println(awardInformationDonation);
		awardInformationDonationService.save(awardInformationDonation);

        return R.ok("success");
    }

    /**
     * 修改
     */
    @PutMapping("/update")

    public R update(AwardInformationDonationEntity awardInformationDonation,@RequestParam("file")MultipartFile multipartFile){
        if (!multipartFile.isEmpty()){
            awardInformationDonation.setAwardImg(uploadAdvice.getUrl(multipartFile));
        }
        System.out.println(awardInformationDonation);
		awardInformationDonationService.updateById(awardInformationDonation);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")

    public R delete(@RequestBody Integer[] awardIds){
		awardInformationDonationService.removeByIds(Arrays.asList(awardIds));

        return R.ok();
    }
    @GetMapping("/getlistbyrid/{rid}")
    public List<AwardInformationDonationEntity> getByRId(@PathVariable("rid") String rid){
        return awardInformationDonationService.getListByRId(rid);
    }

}
